package com.booking.common.utils;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @author Qikai.Lin
 * @date 2023/4/23
 */
public class LocalDateTimeUtils {

    /**
     * 日期格式，年月日时分秒，年月日用横杠分开，时分秒用冒号分开
     */
    public static final String YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";


    private static final DateTimeFormatter dayFmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final DateTimeFormatter minuteFmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    public static LocalDateTime convertFromDayStr(String time){
        return LocalDate.parse(time, dayFmt).atStartOfDay();
    }

    public static LocalDateTime convertFromMonthStr(String time){
        return convertFromDayStr(time + "-01");
    }

    public static String convertToStrFromTime(LocalDateTime time){
        return dayFmt.format(time);
    }
    public static String convertToMinuteStrFromTime(LocalDateTime time) {
        return minuteFmt.format(time);
    }



    public static String localDateTimeToStr(LocalDateTime time, String timeFormat) {
        return DateTimeFormatter.ofPattern(timeFormat).format(time);
    }
    public static String toYyyyMMddStr(LocalDateTime time) {
        return DateTimeFormatter.ofPattern(YYYY_MM_DD).format(time);
    }

    public static Long getDurationDays(LocalDateTime begin, LocalDateTime end) {
        return Duration.between(begin, end).toDays();
    }

    public static Long getDurationHours(LocalDateTime begin, LocalDateTime end) {
        return Duration.between(begin, end).toHours();
    }

    public static Long getDurationMinutes(LocalDateTime begin, LocalDateTime end) {
        return Duration.between(begin, end).toMinutes();
    }

    public static Long getDurationMillis(LocalDateTime begin, LocalDateTime end) {
        return Duration.between(begin, end).toMillis();
    }



    public static LocalDateTime yyyyMMddStrToLocalDateTime(String timeStr, String timeFormat) {
        return LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(timeFormat));
    }

    public static LocalDateTime yyyyMMddStrToLocalDateTime(String timeStr) {
        LocalDate localDate = LocalDate.parse(timeStr, DateTimeFormatter.ofPattern(YYYY_MM_DD));
        return localDate.atStartOfDay();
    }



    /**
     * 时间戳转LocalDateTime
     *
     * @param timestamp 时间戳
     * @return localDateTime
     */
    public static LocalDateTime timestampToLocalDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * LocalDateTime转时间戳
     *
     * @param localDateTime localDateTime
     * @return 时间戳
     */
    public static Long localDateTimeToTimestamp(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zoneId).toInstant();
        return instant.toEpochMilli();//毫秒时间戳
    }



}
